package com.goparry.taskman.inbox;

import java.util.List;

import com.goparry.taskman.R;
import com.goparry.taskman.repository.TaskNote;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


//This ArrayAdapter is responsible for displaying each task note in a ListView

public class TaskNoteAdapter extends ArrayAdapter<TaskNote>{
	int resource;

	public TaskNoteAdapter(Context context, int _resource, int textViewResourceId,
			List<TaskNote> objects) {
		super(context, _resource, textViewResourceId, objects);
		
		//This is a pointer to the layout that displays a task
		resource = _resource; 
	}
	//@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout newView;
		TaskNote taskNote;
		
		// Get the specific task
		taskNote = (TaskNote)this.getItem(position);
		newView = new LinearLayout(getContext());

		// Inflate a new view if this is not an update.
		if (convertView == null) {
			newView = new LinearLayout(getContext());
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
			vi.inflate(resource, newView, true);
		} else {
			newView = (LinearLayout)convertView;
		}
		
		// Populate the drawing resources
		ImageView type = (ImageView)newView.findViewById(R.id.tnicon);
		if ("text".equals(taskNote.getType())) {
			type.setImageResource(R.drawable.ic_task_note_text);
		} else if ("picture".equals(taskNote.getType())) {
			type.setImageResource(R.drawable.ic_task_note_image);
		} else if ("video".equals(taskNote.getType())) {
			type.setImageResource(R.drawable.ic_task_note_video);
		} else if ("voice".equals(taskNote.getType())) {
			type.setImageResource(R.drawable.ic_task_note_voice);
		} else {
			type.setImageResource(R.drawable.ic_task_note_text);
		}
		
		TextView name = (TextView)newView.findViewById(R.id.tnname);
		name.setText(taskNote.getName());
		
		TextView dueDate = (TextView)newView.findViewById(R.id.tnmoddate);
		dueDate.setText(taskNote.getDateMofifiedAsString());
				
		return newView;
	}
	

}
